package com.example.myapplication.base.mvp.ui;

import com.example.myapplication.base.mvp.BasePresenter;
import com.example.myapplication.base.mvp.IBaseUiView;
import com.example.myapplication.utils.Toast;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.impl.LoadingPopupView;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.ability.fraction.FractionManager;
import ohos.aafwk.content.Intent;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

public abstract class BaseAbilitySlice<P extends BasePresenter> extends AbilitySlice implements IBaseUiView {
    protected P mPresenter;

    public abstract int getUIContent();

    public abstract void initComponent();

    protected abstract P createPresenter();
    public void initClickListener(){}
    public Context mContext;

    public Intent intent;


    protected EventHandler eventHandler;

    private Runnable cancleLoadingTast;

    private LoadingPopupView loadingPopup ;
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        this.intent = intent;
        loadingPopup = new XPopup.Builder(this).asLoading();
        eventHandler = new EventHandler(EventRunner.getMainEventRunner());
        cancleLoadingTast = new Runnable() {
            @Override
            public void run() {
                if (loadingPopup != null)
                    loadingPopup.dismiss();
            }
        };
        mContext = this;
        mPresenter = createPresenter();
        mPresenter.bindView(this);
        mPresenter.oncreate();
        beforsetUIContent();
        super.setUIContent(getUIContent());
        this.initComponent();
        initClickListener();
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        if(mPresenter!=null)mPresenter.onStop();
    }

    @Override
    protected void onStop() {
        super.onStop();
      if(mPresenter!=null)mPresenter.onDestroy();
    }


    public String getString(int resId) {
        try {
            return getResourceManager().getElement(resId).getString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public int getColor(int colorId) {
        try {
            return getResourceManager().getElement(colorId).getColor();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    protected void onBackPressed() {
        super.onBackPressed();
        dismissLoadingDialog();
    }

    @Override
    public void dismissLoadingDialog() {
        if(loadingPopup!=null){
            loadingPopup.dismiss();
            eventHandler.removeTask(cancleLoadingTast);
        }
    }

    public FractionManager getFractionManager() {
        Ability ability = getAbility();
        if (ability instanceof FractionAbility) {
            FractionAbility fractionAbility = (FractionAbility) ability;
            return fractionAbility.getFractionManager();
        }
        return null;
    }

    public P getPresenter() {
        return mPresenter;
    }

    public void beforsetUIContent() {

    }

    @Override
    public void showLoadingDialog() {
        if (loadingPopup != null && !loadingPopup.isShow()) {
            loadingPopup.show();
        }
    }

    @Override
    public void showEmptyView() {

    }

    @Override
    public void showErrorMsg(int errorCode, String msg) {
        showToast(msg);
    }

    @Override
    public void showToast(String message) {
        Toast.makeToast(mContext, message, 3000).show();
    }

    @Override
    public void showLoadingDialogNow(boolean isNow) {
        if(isNow){
            showLoadingDialog();
        }else {
            if(loadingPopup != null && !loadingPopup.isShow()) {
                postTask(new Runnable() {
                    @Override
                    public void run() {
                        showLoadingDialog();
                    }
                }, 750);
            }
        }
        if (eventHandler != null) {
            eventHandler.postTask(cancleLoadingTast, 15000);
        }
    }

    @Override
    public void showErrorView() {

    }

    @Override
    public void showNoMoreView() {

    }

    //    @Override
//    public void showLoading(Boolean isShowProgress) {
//
//    }
//    @Override
//    public void hideLoading() {
//
//    }
//    @Override
//    public void onProgress(int progress) {
//
//    }
//    @Override
//    public void onErrorState(BaseModel model, int mType) {
//        if (!BaseContent.getIsTrueCode(model.getCode())) {
//            Toast.show(mContext, model.getMsg());
//        }
//    }
}
